package cn.tedu.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * JDBK执行DML语句；UPDATE
 * 修改密码功能
 * 输入用户名 输入修改的密码
 *修改成功或者修改不存在
 */
public class JDBCDemo2 {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Scanner scanner=new Scanner(System.in);
        Class.forName("com.mysql.cj.jdbc.Driver");
        System.out.println("请输入账号");
        String username=scanner.nextLine();
        System.out.println("请输入密码");
        String passwird=scanner.nextLine();
        Connection connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root","123456");
        Statement statement=connection.createStatement();
        String sql="INSERT INTO userinfo(username, password, nickname, age)\n" +
                "VALUES('456','123','123',18);";
        int i=statement.executeUpdate(sql);
        String sql1="UPDATE userinfo SET password='"+passwird+"' WHERE username='"+username+"'; ";
        int q=statement.executeUpdate(sql1);
        if (q>0){
            System.out.println("修改成功");
        }else {
            System.out.println("未修改成功");
        }
        connection.close();

    }

}
